package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import com.example.demo.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Create by keboom on 2020/6/24
 */
@Controller
public class AdminUserController {

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/admin/user")
    public String empList(Model model){
        List<UserModel> userModels = userMapper.selectAll();
        model.addAttribute("users",userModels);
        return "emp/list";
    }

    @GetMapping("/admin/user/{id}")
    public String putUser(@PathVariable("id")Integer id, HttpSession session,Model model){
        session.setAttribute("id",id);
        UserModel userModel = userMapper.selectById(id);
        model.addAttribute("user",userModel);
        return "emp/add";
    }

    /**修改用户信息
     *
     * @param id
     * @param userName
     * @param userBirthday
     * @param gender
     * @param userBalance
     * @param model
     * @return
     */
    @PutMapping("/admin/user/{id}")
    public String putUser(@PathVariable("id")Integer id,String userName,
                          String userBirthday,String gender,float userBalance,Model model){
        UserModel user = new UserModel();
        user.setUserId(id);
        user.setUserName(userName);
        user.setUserBirthday(userBirthday);
        user.setUserSex(gender);
        user.setUserBalance(userBalance);
        userMapper.updateById(user);
        return "redirect:/admin/user";
    }

    /**删除用户
     *
     * @param id
     * @return
     */
    @GetMapping("/admin/user/delete/{id}")
    public String deleteUser(@PathVariable("id")Integer id){
        userMapper.deleteById(id);
        return "redirect:/admin/user";
    }
}
